package com.qian.ddj.web.controller;

import com.qian.api.ddj.model.Business;
import com.qian.api.ddj.model.Channel;
import com.qian.api.ddj.service.IBusinessService;
import com.qian.api.ddj.service.IChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

@RestController
@Api(tags = "通道管理")
@RequestMapping("/ddj/channel")
public class ChannelController {

    @Autowired
    private IChannelService channelService;
    @Autowired
    private IBusinessService businessService;

    @GetMapping
    @ApiOperation("通道列表")
    public List<Channel> list() {
        List<Channel> channels = channelService.findAll();
        return channels;
    }

    @PostMapping
    @ApiOperation("保存通道")
    public Channel save(Channel channel) {
        channel = channelService.save(channel);
        return channel;
    }

    @GetMapping("/stop/{id}")
    public void stop(@PathVariable Integer id) {
        List<Business> businesses = businessService.findAllBusinessByChannelId(id);
        Set<Integer> businessIds = new HashSet<>();
        for (Business business : businesses) {
            businessIds.add(business.getId());
        }
        businessService.stop(businessIds);
    }

    @GetMapping("/start/{id}")
    public void start(@PathVariable Integer id) {
        List<Business> businesses = businessService.findAllBusinessByChannelId(id);
        Set<Integer> businessIds = new HashSet<>();
        for (Business business : businesses) {
            businessIds.add(business.getId());
        }
        businessService.start(businessIds);
    }
}
